using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class FormUIManager : MonoBehaviour {

    public static FormUIManager Current { get; set; }

    [SerializeField]
    FormUIImage _formUIButtonPrefab;

    [SerializeField]
    Transform _container;

    Dictionary<string, FormUIImage> formIcons = new Dictionary<string, FormUIImage>();

    void OnEnable() {
        Current = this;
    }

    public void AddIcon(string form, FormController formController) {
        GameObject gameObject = Instantiate(_formUIButtonPrefab.gameObject);
        FormUIImage image = gameObject.GetComponent<FormUIImage>();
        FormManager.Form formData = FormManager.Current.GetForm(form);

        image.Form = form;
        image.FormControllerComponent = formController;
        image.IconImage.sprite = formData.icon;

        gameObject.transform.SetParent(_container, false);
        gameObject.SetActive(true);

        formIcons[form] = image;
    }

    public void Select(string form) {
        DeselectAll();

        if(formIcons.ContainsKey(form))
            formIcons[form].Select();
    }

    public void DeselectAll() {
        foreach(FormUIImage formUIImage in formIcons.Values)
            formUIImage.Deselect();
    }
}